﻿using System;
using System.Collections;
using Validity.Rules;
using Validity.Rules.Decorators;

namespace Validity.Contexts
{
  public interface IComparableValidationContext<TObject, TValue>
    : IConditionalValidationContext<TObject, TValue>
    where TValue : IComparable
  {
    IConditionalValidationContext<TObject, TValue> WhenUsingComparer(Comparison<TValue> predicate);
  }

  public class ComparableValidationContext<TObject, TValue>
    : ConditionalValidationContext<TObject, TValue>, IComparableValidationContext<TObject, TValue>
    where TValue : IComparable
  {
    public ComparableValidationContext()
    { }

    public ComparableValidationContext(IValidationContext<TObject, TValue> context)
      : base(context)
    { }

    #region WhenUsingComparer

    public IConditionalValidationContext<TObject, TValue> WhenUsingComparer(IComparer comparer)
    {
      Verify.NotNull(comparer);

      return WhenUsingComparer((lhs, rhs) => comparer.Compare(lhs, rhs));
    }

    public IConditionalValidationContext<TObject, TValue> WhenUsingComparer(Comparison<TValue> comparer)
    {
      Verify.NotNull(comparer);

      DecorateLastRuleWith(rule => new CustomComparisonRuleDecorator<TObject, TValue>((IComparisonRule<TObject, TValue>)rule, comparer));
      
      return new ConditionalValidationContext<TObject, TValue>(this);
    }

    #endregion
  }
}
